package cn.com.callback;

import java.util.Map;
import cn.com.elite.EliteBean;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.com.elite.Util;
import net.sf.json.JSONObject;
import net.sf.json.JSONArray;
/**
 * 
 * @author Rick
 * @use 获取用户信息，写入本地
 *
 *
 */
public class Getphcustomer implements ServiceBackInvoke{
	private static final Log log=LogFactory.getLog(Getphcustomer.class);
	private String charset = "utf-8"; //设置字符集
	private int timeout = 100000; //设置请求超时时间
	private int enableSSLflag = 1;
	public String doCallBack(EliteBean eliteBean, HttpServletRequest request,HttpServletResponse response){
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json");
		JSONObject result = new JSONObject();
		def smap,oJson;
		try {
			//获取客户端传参
			def r_custname = Util.filterForObject(request.getParameter("customername")); //获取客户姓名
			def r_custmobile = Util.filterForObject(request.getParameter("customermobile")); //获取客户手机
			def r_custidno = Util.filterForObject(request.getParameter("customeridno")); //获取身份证号码
			log.debug("NGS传参:"+r_custname);

			//接口调用
			if(r_custname.equals("")&&r_custmobile.equals("")&&r_custidno.equals("")) {
				result.put("code", "-1");
				result.put("msg", "获取用户信息失败！");
				return result
			}else{
				oJson = CustHttpPort(eliteBean,r_custname,r_custmobile,r_custidno); //调用接口方法
				log.debug("接口返回:"+oJson);
			}


		}catch (Exception e) {
			log.error("errormsg:", e)
			result.put("code", "-1");
			result.put("msg", "调用失败！");
			return result;
		}
	}


/**
 * 
 * @use 客户信息处理
 * @param eliteBean
 * @param userId
 * @return
 */
	private String CustHttpPort(EliteBean eliteBean,String Custname,String Mobile,String Idno) {
		JSONObject result = new JSONObject();
		def smap,mopsmap,chatqueue,customer_guid,url="";
		try {
			//获取接口地址
			smap = eliteBean.chaxun_sql("select target_url as url from sys_interface_config where type ='getCustomerinfo'","dataSource");
			if(smap.size()==0) {
				result.put("msg", "找不到对应接口");
				result.put("code", "-1");
				return result;
			}else{
				 url = smap[0].getAt("url");
			};
			//传参处理
			StringBuffer formdata = new StringBuffer();
			formdata.append("key=ca3c64a49d1f11ecbed6d703198897725e7aba55");
			if(!Custname.equals("")){
				formdata.append("&customername="+Custname);
			}else if(!Mobile.equals("")){
				formdata.append("&mobile="+Mobile);
			}else if(!Idno.equals("")){
				formdata.append("&idno="+Idno);
			}
			//生产签名
			def sign= Util.getMD5Str(formdata.toString());
			formdata.append("&sign="+sign);
			def postmap=Util.doHttpPost(url,formdata.toString(),charset,timeout,enableSSLflag);
			JSONObject obj = JSONObject.fromObject(postmap);
			if(obj.getInt("status")!=200) {
				result.put("code",obj.getInt("code"));
				result.put("msg", "接口调用异常");
				return result;
			}else{
				return obj;
			};

		}catch (Exception e) {
			log.error("errormsg:", e)
			result.put("code", "-1");
			result.put("msg", "接口调用异常");
			return result;
		}
	}


}
